package com.ruoyi.jinchuguanli.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.cangku.mapper.KucunguanliMapper;
import com.ruoyi.cangku.mapper.ShangpinguanliMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gongjulei;
import com.ruoyi.jinchuguanli.controller.JasperUtils;
import com.ruoyi.jinchuguanli.domain.ShangPinCun;
import com.ruoyi.jinchuguanli.domain.Xiaoshouchuku;
import com.ruoyi.jinchuguanli.domain.XiaoshouchukuReturn;
import com.ruoyi.jinchuguanli.mapper.XiaoshouchukuMapper;
import com.ruoyi.jinchuguanli.service.IXiaoshouchukuService;
import net.sf.jasperreports.engine.JRException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

import static com.ruoyi.gongjulei.aReturnFormat;
import static com.ruoyi.gongjulei.returnGet;

/**
 * chukuService业务层处理
 *
 * @author ruoyi
 * @date 2023-11-08
 */
@Service
public class XiaoshouchukuServiceImpl implements IXiaoshouchukuService {
    @Autowired
    private XiaoshouchukuMapper xiaoshouchukuMapper;

    @Autowired
    private KucunguanliMapper kucunguanliMapper;

    @Autowired
    private ShangpinguanliMapper shangpinguanliMapper;

    /**
     * 查询chuku
     *
     * @param id chuku主键
     * @return chuku
     */
    @Override
    public Xiaoshouchuku selectXiaoshouchukuById (Long id) {
        return xiaoshouchukuMapper.selectXiaoshouchukuById(id);
    }

    /**
     * 查询chuku列表
     *
     * @param xiaoshouchuku chuku
     * @return chuku
     */
    @Override
    public List<Xiaoshouchuku> selectXiaoshouchukuList (Xiaoshouchuku xiaoshouchuku) {
        return xiaoshouchukuMapper.selectXiaoshouchukuList(xiaoshouchuku);
    }

    /**
     * 新增chuku
     *
     * @param xiaoshouchuku chuku
     * @return 结果
     */
    @Override
    public int insertXiaoshouchuku (Xiaoshouchuku xiaoshouchuku) {
        if (gongjulei.peitao(xiaoshouchuku,shangpinguanliMapper)) return 6;
        XiaoshouchukuReturn xiaoshouchukuReturn = gongjulei.aReturnFormat(xiaoshouchuku,shangpinguanliMapper);
        gongjulei.kucunUpdate(JSONObject.toJSONString(xiaoshouchukuReturn.getShangpin()), kucunguanliMapper, "-");
        List<ShangPinCun> shangPinCunList = JSONArray.parseArray(xiaoshouchuku.getShangpinbianhao(),ShangPinCun.class);
        xiaoshouchuku.setShangpinbianhao(JSONObject.toJSONString(shangPinCunList));
        return xiaoshouchukuMapper.insertXiaoshouchuku(xiaoshouchuku);
    }


    /**
     * 修改chuku
     *
     * @param xiaoshouchuku chuku
     * @return 结果
     */
    @Override
    public int updateXiaoshouchuku (Xiaoshouchuku xiaoshouchuku) {
        if (gongjulei.peitao(xiaoshouchuku,shangpinguanliMapper)) return 6;
        Xiaoshouchuku xiaoshouchuku1 = xiaoshouchukuMapper.selectXiaoshouchukuById(xiaoshouchuku.getId());
        gongjulei.kucunUpdate(xiaoshouchuku.getShangpinbianhao(), xiaoshouchuku1.getShangpinbianhao(), kucunguanliMapper, "-");
        List<ShangPinCun> shangPinCunList = JSONArray.parseArray(xiaoshouchuku.getShangpinbianhao(),ShangPinCun.class);
        xiaoshouchuku.setShangpinbianhao(JSONObject.toJSONString(shangPinCunList));
        return xiaoshouchukuMapper.updateXiaoshouchuku(xiaoshouchuku);
    }

    /**
     * 批量删除chuku
     *
     * @param ids 需要删除的chuku主键
     * @return 结果
     */
    @Override
    public int deleteXiaoshouchukuByIds (String ids) {
        String[] strings = Convert.toStrArray(ids);
        for (String id:strings
             ) {
            Xiaoshouchuku xiaoshouchuku = xiaoshouchukuMapper.selectXiaoshouchukuById(Long.valueOf(id));
            XiaoshouchukuReturn xiaoshouchukuReturn = gongjulei.aReturnFormat(xiaoshouchuku,shangpinguanliMapper);
            gongjulei.kucunUpdate(JSONObject.toJSONString(xiaoshouchukuReturn.getShangpin()),kucunguanliMapper,"+");
        }
        return xiaoshouchukuMapper.deleteXiaoshouchukuByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除chuku信息
     *
     * @param id chuku主键
     * @return 结果
     */
    @Override
    public int deleteXiaoshouchukuById (Long id) {
        Xiaoshouchuku xiaoshouchuku = xiaoshouchukuMapper.selectXiaoshouchukuById(id);
        XiaoshouchukuReturn xiaoshouchukuReturn = gongjulei.aReturnFormat(xiaoshouchuku,shangpinguanliMapper);
        gongjulei.kucunUpdate(JSONObject.toJSONString(xiaoshouchukuReturn.getShangpin()),kucunguanliMapper,"+");
        return xiaoshouchukuMapper.deleteXiaoshouchukuById(id);
    }

    @Override
    public List<XiaoshouchukuReturn> selectXiaoshouchukuReturnList (Xiaoshouchuku xiaoshouchuku) {
        List<Xiaoshouchuku> xiaoshouchukuList = xiaoshouchukuMapper.selectXiaoshouchukuList(xiaoshouchuku);
        return returnGet(xiaoshouchukuList,shangpinguanliMapper);
    }

//    @Override
//    public List<XiaoshouchukuReturn> selectXiaoshouchukuReturnList(XiaoshouchukuReturn XiaoshouchukuReturn) {
//        List<Xiaoshouchuku> xiaoshouchukuList = xiaoshouchukuMapper.selectXiaoshouchukuReturnList(XiaoshouchukuReturn);
//
//        return returnGet(xiaoshouchukuList);
//    }

    @Override
    public int updateXiaoshouchukuReturn (XiaoshouchukuReturn xr) {
        Xiaoshouchuku xiaoshouchuku = new Xiaoshouchuku();
        BeanUtils.copyProperties(xr, xiaoshouchuku);
//        String jsonStr = JSONObject.toJSONString(xr.getShangpin());
//        xiaoshouchuku.setShangpinbianhao(jsonStr);
        List<ShangPinCun> shangPinCunList = JSONArray.parseArray(JSONObject.toJSONString(xr.getShangpin()),ShangPinCun.class);
        xiaoshouchuku.setShangpinbianhao(JSONObject.toJSONString(shangPinCunList));
        return xiaoshouchukuMapper.updateXiaoshouchuku(xiaoshouchuku);
    }

    @Override
    public String updateChuKuDan(Xiaoshouchuku xc) {
        try {
         return JasperUtils.ChuKuDanSet(xc,shangpinguanliMapper);
        } catch (IOException | JRException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public XiaoshouchukuReturn aReturnFormat(Xiaoshouchuku xiaoshouchuku) {
        return gongjulei.aReturnFormat(xiaoshouchuku,shangpinguanliMapper);
    }

    @Override
    public int selectIdForNewOne() {
        return xiaoshouchukuMapper.selectIdForNewOne();
    }

    @Override
    public List<SysUser> selectUserList() {
        return xiaoshouchukuMapper.selectUserList();
    }


}
